//
//  HSYShopInfoViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/14.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYShopInfoViewController.h"
#import "HSYShopInfoQrCell.h"
#import "HSYShopInfoHeaderView.h"
#import "HSYShopInfoSectionView.h"
#import "HSYShopInfoClerkCell.h"
#import "HSYShopinfoNoDataCell.h"
#import "HSYShopDetailInfoModel.h"
#import "HSYGetShopInfoRequest.h"
#import "BQActivityView.h"
#import "HSYQRViewController.h"

static NSString *const qrCellReuseIdentifier = @"qrCellReuseIdentifier";
static NSString *const clerkCellReuseIdentifier = @"clerkCellReuseIdentifier";
static NSString *const shopReuseIdentifier = @"shopReuseIdentifier";
static NSString *const headerReuseIdentifier = @"headerReuseIdentifier";
static NSString *const noDataReuseIdentifier = @"noDataReuseIdentifier";

@interface HSYShopInfoViewController ()

@property (nonatomic, strong) HSYShopDetailInfoModel *model;

@end

@implementation HSYShopInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.titleButton setTitle:@"店铺信息" forState:UIControlStateNormal];
    
    [self registerCell];
    [self loadServerData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:@"HSYShopInfoHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:shopReuseIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"HSYShopInfoQrCell" bundle:nil] forCellReuseIdentifier:qrCellReuseIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"HSYShopInfoClerkCell" bundle:nil] forCellReuseIdentifier:clerkCellReuseIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"HSYShopInfoSectionView" bundle:nil] forHeaderFooterViewReuseIdentifier:headerReuseIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"HSYShopinfoNoDataCell" bundle:nil] forCellReuseIdentifier:noDataReuseIdentifier];
}

- (BOOL)hsy_allowPullUp{
    return NO;
}

- (BOOL)hsy_allowPullDown{
    return NO;
}

-(void)loadServerData{
    HSYGetShopInfoRequest *task = [[HSYGetShopInfoRequest alloc] init];
    task.shopId = self.shopModel.shopID;
    
    [BQActivityView showActiviTy];
    __weak typeof(self)weakSelf = self;
    
    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        [BQActivityView hideActiviTy];
        
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            weakSelf.model = [HSYShopDetailInfoModel mj_objectWithKeyValues:response.encryptDataResult];
            
            [weakSelf.tableView reloadData];
        }else{
            [weakSelf showMessage:response.resultMessage];
        }
        
    } failure:^(NSError *error) {
        [BQActivityView hideActiviTy];
        
        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];
    
}

- (void)pushToQR:(HSYQrModel *)model{
    model.qrUrl = self.model.qrUrl;
    model.shopName = self.shopModel.shortName.length > 0 ? self.shopModel.shortName : self.shopModel.name;
    
    HSYQRViewController *vc = [[HSYQRViewController alloc] init];
    vc.qrModel = model;
    
    [self.navigationController pushViewController:vc animated:YES];    
}


#pragma mark - UITableViewDelegate, UITableViewDataSource -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return MAX(1, self.model.qrList.count);
    }
    
    if (section == 2) {
        return  MAX(1, self.model.userList.count);
    }
    
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1 && self.model.qrList.count > 0) {
        HSYQrModel *model = self.model.qrList[indexPath.row];
        
        HSYShopInfoQrCell *cell = [tableView dequeueReusableCellWithIdentifier:qrCellReuseIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.codeLabel.text = model.code;
        cell.dateLabel.text = [NSString dateStringFrom1970:model.createtime/1000 formatter:@"yyyy-MM-dd HH:mm:ss"];
        cell.lineView.hidden = indexPath.row == self.model.qrList.count-1 ? NO : YES;
        
        __weak typeof(self) wSelf = self;
        cell.qrClickBlock = ^{
            [wSelf pushToQR:model];
        };
        
        return cell;
    }else if(indexPath.section == 1){
        HSYShopinfoNoDataCell *cell = [tableView dequeueReusableCellWithIdentifier:noDataReuseIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = @"暂未绑定二维码";
        return cell;
    }
    
    if (indexPath.section == 2 && self.model.userList.count > 0) {
        HSYClerkInfoModel *model = self.model.userList[indexPath.row];
        HSYShopInfoClerkCell *cell = [tableView dequeueReusableCellWithIdentifier:clerkCellReuseIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.nameLabel.text = model.realname;
        cell.phoneLabel.text = model.cellphone;
        
        cell.roleLabel.text = model.role == 2 ? @"店长" : @"店员";
        cell.roleLabel.backgroundColor = model.role == 2 ? [UIColor hsyShopOwnerBgColor] : [UIColor hsyShopEmployeeBgColor];
        cell.lineView.hidden = indexPath.row == self.model.userList.count-1 ? NO : YES;
        
        return cell;

    }else{
        HSYShopinfoNoDataCell *cell = [tableView dequeueReusableCellWithIdentifier:noDataReuseIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = @"暂无员工";
        return cell;

    }
    
    return nil;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        HSYShopInfoHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:shopReuseIdentifier];
        
        if (self.model.appBizShop.type == 1) {
            view.shopNameLabel.text = self.model.appBizShop.name;
            view.hostStatusLabel.hidden = NO;
        }else{
            view.shopNameLabel.text = self.model.appBizShop.shortName;
            view.hostStatusLabel.hidden = YES;
        }
        view.addressLabel.text = self.model.appBizShop.address;
        view.qrCountLabel.text = [NSString stringWithFormat:@"%ld", (long)self.model.appBizShop.countQR];
        view.employeeCountLabel.text = [NSString stringWithFormat:@"%ld", (long)self.model.appBizShop.countEmployee];
        
        return view;
    }
    
    if (section == 1 || section == 2) {
        HSYShopInfoSectionView * view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier];
        
        view.iconImageView.image = section == 1 ? [UIImage imageNamed:@"shopinfo_qr"] : [UIImage imageNamed:@"shopinfo_clerk"];
        view.titleLabel.text = section == 1 ? @"二维码" : @"员工";
        
        return view;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.model) {
        return 30;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 && self.model) {
        return 100;
    }
    
    if (section == 1 && self.model) {
        return 51;
    }
    
    if (section == 2 && self.model) {
        return 51;
    }
    return 0;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
